/**
 * 
 */
package org.xelnaga.defiler.web.servlet.view;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import freemarker.cache.TemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.TemplateException;

/**
 * @author qijb
 *
 */
public class WebsiteFreeMarkerConfigurer extends FreeMarkerConfigurer {
	private Map<String, String> macroLibraries;
	/**
	 * 
	 */
	public WebsiteFreeMarkerConfigurer() {
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void postProcessConfiguration(Configuration config)
			throws IOException, TemplateException {
		super.postProcessConfiguration(config);
		if (macroLibraries != null) {
			for(Map.Entry<String, String> entry : macroLibraries.entrySet()) {
				config.addAutoImport(entry.getKey(), entry.getValue());
			}
		}		
	}

	@Override
	protected void postProcessTemplateLoaders(List<TemplateLoader> templateLoaders) {
		super.postProcessTemplateLoaders(templateLoaders);
		templateLoaders.add(new ResourceTemplateLoader(getResourceLoader()));
	}

	public void setMacroLibraries(Map<String, String> macroLibraries) {
		this.macroLibraries = macroLibraries;
	}

}
